package suite.test
{
	import com.aflac.event.SharedServicesEvent;
	import com.aflac.service.SharedServices;
	import com.aflac.util.LoaderUtil;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import flexunit.framework.Assert;
	
	import org.flexunit.async.Async;
	
	public class SharedServicesShould
	{
		/**
		 * @TODO: set the PRDODUCTION base url
		 * @TODO: determine whether more granular assertions need to be made in async handlers
		 * @TODO: determine conditions that will produce a 'statusCode' service response other than '1' for each service 
		 * 			- see tests with [Ignore]
		 **/ 
		public static const STAGING:String = "http://aflacdev.digitas.com/services";
		public static const PRODUCTION:String = "http://aflacdev.digitas.com/services";
		
		private var service:SharedServices;
		
		
		//--------------------------------------------------------------------------
		//
		//  Before and After
		//
		//--------------------------------------------------------------------------
		
		[Before]
		public function setUp():void 
		{

			service = new SharedServices();
			service.dispatcher = new EventDispatcher();
			service.loaderUtil = new LoaderUtil();
	
		}
		
		[After]
		public function tearDown():void 
		{
			service = null;
		}       
		
		//--------------------------------------------------------------------------
		//
		//  Tests
		//
		//--------------------------------------------------------------------------
		
		/**
		 * All Services
		 **/ 
		//this passes when a call to a non-existent service produces a SERVICE_CALL_FAILED event 
		//from the shared services class
		//pass in a non-existant service url to any of the services
		[Ignore("ignore until figure out a way to throw failed event")][Test(async,timeout="500")]
		public function throwFailedEvent_ForSharedServiceFailed():void 
		{
			service.dispatcher.addEventListener(SharedServicesEvent.SERVICE_CALL_FAILED, 
				Async.asyncHandler( this, onServiceCallFailed, 500 ), false, 0, true );
			
			service.attemptLogin();
		}
		
		/**
		 * Login
		 **/ 
		//this test passes when valid credentials produce a LOGIN_VALID event from the shared services class
		[Test(async,timeout="500")]
		public function throwValidEvent_ForValidCredentials():void 
		{
			service.dispatcher.addEventListener(SharedServicesEvent.LOGIN_VALID, 
				Async.asyncHandler( this, onValidLogin, 500 ), false, 0, true );
				
			requestVars.deviceType = "air";
			requestVars.Username = "replaceMeWithValidUserName";
			requestVars.Password = "replaceMeWithValidPassword";
			
			service.attemptLogin();
		}
		
		//this test passes when invalid credentials produce a LOGIN_INVALID event from the shared services class
		[Ignore("Ignore until service can return 'invalid' status")]
		[Test(async,timeout="500")]
		public function throwInvalidEvent_ForInvalidCredentials():void 
		{
			service.dispatcher.addEventListener(SharedServicesEvent.LOGIN_INVALID, 
				Async.asyncHandler( this, onInvalidLogin, 500 ), false, 0, true );
				
			requestVars.deviceType = "air";
			requestVars.Username = "replaceMeWithInvalidUserName";
			requestVars.Password = "replaceMeWithInvalidPassword";
			
			service.attemptLogin();
		}
		
		/**
		 * Content Version
		 **/ 
		//this test passes when a request for the current content version on the server
		//produces a CONTENT_VERSION_SUCCESS event from the shared services class
		[Test(async,timeout="500")]
		public function throwSuccessEvent_ForContentVersionSuccess():void 
		{
			service.dispatcher.addEventListener(SharedServicesEvent.CONTENT_VERSION_SUCCESS, 
				Async.asyncHandler( this, onContentVersionSuccess, 500 ), false, 0, true );
				
			requestVars.deviceType = "air";
			requestVars.screenID = "screenID";
			requestVars.requestType = "requestType";
			
			service.getContentVersion();
		}
		//this test passes when a request for the current content version on the server
		//produces a CONTENT_VERSION_FAILED event from the shared services class
		[Ignore("Ignore until conditions for this service to return 'invalid' status can be determined")]
		[Test(async,timeout="500")]
		public function throwFailedEvent_ForContentVersionFailed():void 
		{
			service.dispatcher.addEventListener(SharedServicesEvent.CONTENT_VERSION_FAILED, 
				Async.asyncHandler( this, onContentVersionFailed, 500 ), false, 0, true );
				
			requestVars.deviceType = "air";
			requestVars.screenID = "screenID";
			requestVars.requestType = "requestType";
			
			service.getContentVersion();
		}
		
		/**
		 * Content Data
		 **/ 
		//this test passes when a request for content data from the server
		//produces a CONTENT_DATA_SUCCESS event from the shared services class
		[Test(async,timeout="500")]
		public function throwSuccessEvent_ForContentDataSuccess():void 
		{
			service.dispatcher.addEventListener(SharedServicesEvent.CONTENT_DATA_SUCCESS, 
				Async.asyncHandler( this, onContentDataSuccess, 500 ), false, 0, true );
			
			requestVars.deviceType = "air";
			requestVars.screenID = "screenID";
			requestVars.requestType = "requestType";
			requestVars.version = "0.0";
			
			service.getContentData();
		}
		//this test passes when a request for content data from the server
		//produces a CONTENT_DATA_FAILED event from the shared services class
		[Ignore("Ignore until conditions for this service to return 'invalid' status can be determined")]
		[Test(async,timeout="500")]
		public function throwFailedEvent_ForContentDataFailed():void 
		{
			service.dispatcher.addEventListener(SharedServicesEvent.CONTENT_DATA_FAILED, 
				Async.asyncHandler( this, onContentDataFailed, 500 ), false, 0, true );
				
			requestVars.deviceType = "air";
			requestVars.screenID = "screenID";
			requestVars.requestType = "requestType";
			requestVars.version = "0.0";
			
			service.getContentData();
		}
		
		/**
		 * Presentations
		 **/ 
		//this test passes when a request for presentations from the server
		//produces a PRESENTATIONS_SUCCESS event from the shared services class
		[Test(async,timeout="500")]
		public function throwSuccessEvent_ForPresentationsSuccess():void 
		{
			service.dispatcher.addEventListener(SharedServicesEvent.PRESENTATIONS_SUCCESS, 
				Async.asyncHandler( this, onPresentationsSuccess, 500 ), false, 0, true );
				
			requestVars.deviceType = "air";
			
			service.getPresentations();
		}
		//this test passes when a request for presentations from the server
		//produces a PRESENTATIONS_FAILED event from the shared services class
		[Ignore("Ignore until conditions for this service to return 'invalid' status can be determined")]
		[Test(async,timeout="500")]
		public function throwFailedEvent_ForPresentationsFailed():void 
		{
			service.dispatcher.addEventListener(SharedServicesEvent.PRESENTATIONS_FAILED, 
				Async.asyncHandler( this, onPresentationsFailed, 500 ), false, 0, true );
			
			requestVars.deviceType = "air";
			
			service.getPresentations();
		}
		
		

		//--------------------------------------------------------------------------
		//
		//  Asynchronous handlers
		//
		//--------------------------------------------------------------------------
		
		/**
		 * All Services
		 **/ 
		private function onServiceCallFailed(event:SharedServicesEvent, passThroughData:Object):void
		{
			Assert.assertTrue( event.hasOwnProperty("data") );
		}
		
		/**
		 * Login
		 **/ 
		private function onValidLogin(event:SharedServicesEvent, passThroughData:Object):void
		{
			Assert.assertTrue( event.hasOwnProperty("data") );
		}
		private function onInvalidLogin(event:SharedServicesEvent, passThroughData:Object):void
		{
			Assert.assertTrue( event.hasOwnProperty("data") );
		}
		
		/**
		 * Content Version
		 **/ 
		private function onContentVersionSuccess(event:SharedServicesEvent, passThroughData:Object):void
		{
			Assert.assertTrue( event.hasOwnProperty("data") );
		}
		private function onContentVersionFailed(event:SharedServicesEvent, passThroughData:Object):void
		{
			Assert.assertTrue( event.hasOwnProperty("data") );
		}
			

		/**
		 * Content Data
		 **/ 
		private function onContentDataSuccess(event:SharedServicesEvent, passThroughData:Object):void
		{
			Assert.assertTrue( event.hasOwnProperty("data") );
		}
		private function onContentDataFailed(event:SharedServicesEvent, passThroughData:Object):void
		{
			Assert.assertTrue( event.hasOwnProperty("data") );
		}
			

		/**
		 * Presentations
		 **/ 
		private function onPresentationsSuccess(event:SharedServicesEvent, passThroughData:Object):void
		{
			Assert.assertTrue( event.hasOwnProperty("data") );
		}
		private function onPresentationsFailed(event:SharedServicesEvent, passThroughData:Object):void
		{
			Assert.assertTrue( event.hasOwnProperty("data") );
		}
		

		
	}
}